package com.longjianghuasheng.logistics.business.order.adapter

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.databinding.ItemOrderBinding
import com.longjianghuasheng.logistics.entity.Order
import com.longjianghuasheng.logistics.ext.loadUrl
import com.longjianghuasheng.logistics.ext.navOptions
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

class OrderAdapter :
    BaseQuickAdapter<Order, BaseDataBindingHolder<ItemOrderBinding>>(R.layout.item_order) {

    var type = 0

    var onClickListener: (Int, String) -> Unit = { _, _ ->

    }

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseDataBindingHolder<ItemOrderBinding>, item: Order) {
        val flowTagAdapter = FlowTagOrderAdapter(context)
        holder.dataBinding?.run {

            flowlayout.adapter = flowTagAdapter

            flowTagAdapter.onClickListener = {

                onClickListener.invoke(holder.layoutPosition, it)
            }

            ivPic.loadUrl(item.picurl)
            tvTitle.text = item.service_name

            tvStatus.text = when (item.status) {
                STATUS_TO_SIGN_UP -> {
                    flowTagAdapter.addTags(
                        arrayOf(
                            BT_SIGN_UP_ENTRANCE,
                            BT_ADDITIONAL_SERVICES,
                            BT_CANCEL
                        )
                    )
                    "待签约"
                }
                STATUS_FOR_THE_PAYMENT -> {

                    flowTagAdapter.addTags(
                        mutableListOf<String?>().apply {

                            if (type == 0) {
                                add(BT_SIGNED)
                            }
                            add(BT_ADDITIONAL_SERVICES)

                            add(BT_CANCEL)

                        }.toTypedArray()
                    )

                    "待付款"
                }
                STATUS_TO_START -> {
                    flowTagAdapter.addTags(
                        mutableListOf<String?>().apply {


                            if (type == 0) {
                                add(BT_SIGNED)
                                add(BT_ADDITIONAL_SERVICES)
                                add(BT_TRAJECTORY)
//                                add(BT_CONFIRM_THE_RECEIPT_OF_GOODS)

                            } else {
                                add(BT_ADDITIONAL_SERVICES)

                            }

                        }.toTypedArray()

                    )

                    "待开始"
                }
                STATUS_UNDERWAY -> {
                    flowTagAdapter.addTags(
                        mutableListOf<String?>().apply {

                            if (type == 0) {
                                add(BT_SIGNED)
                                add(BT_TRAJECTORY)
                                add(BT_ADDITIONAL_SERVICES)
                                add(BT_CONFIRM_THE_RECEIPT_OF_GOODS)

                            } else {
                                add(BT_ADDITIONAL_SERVICES)
                                add(BT_CONFIRM_COMPLETED)
                            }

                        }.toTypedArray()
                    )

                    "进行中"
                }
                STATUS_COMPLETED -> {

                    flowTagAdapter.addTags(
                        mutableListOf<String?>().apply {

                            if (type == 0) {
                                add(BT_SIGNED)
                                add(BT_TRAJECTORY)
                            }
                            add(BT_DELETE)

                        }.toTypedArray()
                    )

                    "已完成"
                }
                else -> ""
            }
            tvMoney.text = "￥${item.price}"

            tvOrderNum.text = "订单编号：${item.trade_no ?: ""}"
            tvOrderDate.text = "下单日期：${item.create_time}"
            tvContainerNo.run {

                isGone = item.box_no.isNullOrBlank()
                text = "集装箱号：${item.box_no}"
            }
            tvBoxReceive.run {

                isGone = item.box_receive.isNullOrEmpty()
                text = "提单号：${item.box_receive}"
            }


        }

        holder.itemView.setOnClickListener {

            nav(it).navigateAction(R.id.orderDetailFragment, Bundle().apply {
                putInt("id", item.id ?: 0)
                putInt("type", type)

            }, navOptions = navOptions())


        }
    }

    companion object {


        const val BT_SIGN_UP_ENTRANCE = "签约入口"
        const val BT_CANCEL = "取消"
        const val BT_SIGNED = "已签约"
        const val BT_PAYMENT = "付款"
        const val BT_TRAJECTORY = "查看轨迹"
        const val BT_DELETE = "删除订单"
        const val BT_CONFIRM_COMPLETED = "确认完成"

        const val BT_CONFIRM_THE_RECEIPT_OF_GOODS = "确认收货"
        const val BT_ADDITIONAL_SERVICES = "追加服务"

        const val STATUS_TO_SIGN_UP = 1
        const val STATUS_FOR_THE_PAYMENT = 2
        const val STATUS_TO_START = 3
        const val STATUS_UNDERWAY = 4
        const val STATUS_COMPLETED = 5
    }

}